﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;

namespace ASoft.Base
{
    public class ConfigInfo
    {
        static ConfigInfo() { }

        public static string RootPath
        {
            get
            {
                var result = ConfigurationManager.AppSettings["RootPath"];
                if (string.IsNullOrWhiteSpace(result))
                {
                    return AppDomain.CurrentDomain.BaseDirectory;
                }
                return result;
            }
        }

        public static string AccessConnectionString
        {
            get
            {
                var result = ConfigurationManager.AppSettings["AccessConnectionString"];
                if (string.IsNullOrWhiteSpace(result))
                {
                    return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User Id=admin;Password=;";
                }
                return result;
            }
        }

        public static string GetFullPath(string path)
        {
#if DEBUG
            if (String.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["RootPath"]))
            {
                return Path.Combine(@"C:\ASOFT\", path);
            }
#endif
            return Path.Combine(RootPath, path);
        }

        public static T GetValue<T>(string key, T defaultValue)
        {
            var result = ConfigurationManager.AppSettings[key];
            if (string.IsNullOrWhiteSpace(key))
            {
                return defaultValue;
            }
            return (T)System.ComponentModel.TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(result);
        }
    }
}
